package com.txunda.zhuohired.fgt.user.home;

import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ants.theantsgo.config.Settings;
import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tool.ToolKit;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.user.home.CompanyBean;
import com.txunda.zhuohired.http.pst.MemberHomePst;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/5 11:18
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class CompanyFgt extends BaseFgt {

    /**
     * 公司简介
     */
    @ViewInject(R.id.tv_company_intro)
    private TextView tv_company_intro;
    /**
     * 公司地址
     */
    @ViewInject(R.id.tv_company_address)
    private TextView tv_company_address;
    /**
     * 工作环境
     */
    @ViewInject(R.id.iv_company_environment)
    private ImageView iv_company_environment;
    /**
     * 成立日期
     */
    @ViewInject(R.id.tv_company_time)
    private TextView tv_company_time;
    /**
     * 营业状态
     */
    @ViewInject(R.id.tv_company_status)
    private TextView tv_company_status;
    /**
     * 企业类型
     */
    @ViewInject(R.id.tv_company_type)
    private TextView tv_company_type;
    /**
     * 数据来源
     */
    @ViewInject(R.id.tv_company_from)
    private TextView tv_company_from;

    private String u_id = "";
    private int status_type = 1;
    private String token = "";
    private MemberHomePst memberHomePst;
    private int width;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        width = (Settings.displayWidth - ToolKit.dip2px(getActivity(), 24)) * 4 / 5; // 计算控件宽度
    }

    @Override
    protected void immersionInit() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_company;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        memberHomePst = new MemberHomePst(this);
    }

    @Override
    protected void requestData() {

    }

    //    /**
    //     * 当Fragment显示是运行该方法
    //     *
    //     * @param hidden
    //     */
    //    @Override
    //    public void onHiddenChanged(boolean hidden) {
    //
    //        if (hidden) {
    //            //界面不可见
    //        } else {
    //            // 界面可见
    //            L.e("=====可见=====");
    //            initHttp();
    //
    //        }
    //    }

    /**
     * 每次fragment可见时
     * <p/>
     * 请求接口
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    initHttp();
                }
            }, 100);
        } else {
            // 不可见时不执行操作
        }
    }

    private void initHttp() {
        memberHomePst.companyInfo(u_id, String.valueOf(status_type), token);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("companyInfo")) {
            //            L.e("==========companyInfo==========", jsonStr);
            CompanyBean companyBean = GsonUtil.GsonToBean(jsonStr, CompanyBean.class);
            ViewGroup.LayoutParams lp = iv_company_environment.getLayoutParams();
            lp.width = Settings.displayWidth - ToolKit.dip2px(getActivity(), 24);
            lp.height = width;
            iv_company_environment.setLayoutParams(lp);
            iv_company_environment.setMaxWidth(Settings.displayWidth - ToolKit.dip2px(getActivity(), 24));
            iv_company_environment.setMaxHeight(width);
            iv_company_environment.setMinimumWidth(Settings.displayWidth - ToolKit.dip2px(getActivity(), 24));
            iv_company_environment.setMinimumHeight(width);
            iv_company_environment.setVisibility(View.VISIBLE);
            Glide.with(this).load(companyBean.getData().getInfo_data().getCompany_pic()).error(R.mipmap.icon_defult_bg)
                    .placeholder(R.mipmap.icon_defult_bg).dontAnimate().into(iv_company_environment);
            if (TextUtils.isEmpty(companyBean.getData().getInfo_data().getAddress())) {
                tv_company_address.setVisibility(View.GONE);
            } else {
                tv_company_address.setVisibility(View.VISIBLE);
                tv_company_address.setText(companyBean.getData().getInfo_data().getAddress());
            }
            if (TextUtils.isEmpty(companyBean.getData().getInfo_data().getCompany_desc())) {
                tv_company_intro.setVisibility(View.GONE);
            } else {
                tv_company_intro.setVisibility(View.VISIBLE);
                tv_company_intro.setText(Html.fromHtml(companyBean.getData().getInfo_data().getCompany_desc()));
            }
            return;
        }
    }

    public static CompanyFgt getFgt(int type, String u_id) {
        CompanyFgt procurerFgt = new CompanyFgt();
        procurerFgt.u_id = u_id;
        procurerFgt.status_type = type;
        return procurerFgt;
    }
}
